home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / comma2.arc / COMMA2.PAS
Encoding:
Pascal/Delphi Source File  |  1986-09-02  |  2.4 KB  |  68 lines

  1. {                                            9/2/86
  2.   This code allows you to format numeric I/O in Turbo Pascal.
  3. It is based on a routine I first saw in PC Magazine which I
  4. have modified and added to.  It also uses a routine called,
  5. Strip, which I first saw in the Boosters utilities.  Comma2
  6. allows you to format numeric output as currency, percentages,
  7. or in punctuated form.  Comma2 calls upon the Function 'Strip'
  8. to delete leading and trailing characters from the string sent
  9. to it.  By doing that, Comma2 left justifies the number passed
  10. to it for the display field.
  11.   While I haven't tried it yet, I'm sure Comma2 would make a nice
  12. addition to the Boosters utility library.  I have tried this code
  13. with FastWrite, and it works just fine.
  14.   I have included a short program to demonstrate the Comma2
  15. Function.  If you have any comments or questions, please contact
  16. me, John Wood, on the PC-Library BBS.  The code is as follows: }
  17.  
  18.  
  19. Program TestNum;
  20. {$V-}
  21. Type
  22.   Str32  = String[32];
  23.   Str80  = String[80];
  24.   AnyStr = String[255];
  25. Var
  26.   Num    : Real;
  27.  
  28. Function Strip (Str:AnyStr; Ch:Char) : AnyStr;
  29.    { Removes all leading and trailing C characters from S }
  30. Begin
  31.    InLine ($1E/ $8D/$7E/$07/ $8A/$4E/$06/ $30/$ED/ $8C/$D0/ $8E/$C0/
  32.            $8B/$46/$04/ $83/$F9/$01/ $77/$0E/ $8A/$5E/$07/ $30/$FF/
  33.            $39/$D8/ $74/$35/ $8B/$D7/ $EB/$1D/$90/ $FC/ $F3/$AE/ $E3/$2B/
  34.            $4F/ $8B/$D7/ $8A/$4E/$06/ $30/$ED/ $8D/$7E/$07/ $01/$CF/ $4F/
  35.            $FD/ $F3/$AE/ $47/ $8B/$CF/ $29/$D1/ $41/ $88/$8E/$06/$01/
  36.            $8B/$F2/ $8D/$BE/$07/$01/ $8C/$D0/ $8E/$D8/ $FC/ $F3/$A4/
  37.            $EB/$07/$90/ $C7/$86/$06/$01/$00/$00/ $1F/$5D);
  38. End;
  39.  
  40. Function Comma2 (Form:Char; Number:Real; Field:Integer; Dec:Integer) : Str32;
  41. Var
  42.   Hold : Str32;
  43.   I    : Integer;
  44. Begin
  45.   Str (Number:Field:Dec, Hold);
  46.   If Dec > 0 Then Dec := Dec + 1;
  47.   For I := 1 to (Field - Dec - 3) do
  48.     If ((Field - Dec - I) Mod 3 = 0) And (Hold[I] <> ' ')
  49.     And (Hold[I] <> '-') Then
  50.     Begin
  51.       Delete (Hold,1,1); Insert (',', Hold, I);
  52.     End;
  53.   Case Form of
  54.     '$' : Comma2:= '$'+ Strip(Hold,' ');
  55.     '#' : Comma2:= Strip(Hold,' ');
  56.     '%' : Comma2:= Strip(Hold,' ')+' %';
  57.   End;
  58. End;
  59.  
  60. Begin                 { Main Program }
  61.   ClrScr;
  62.   GotoXY(20,5); Write('What is the Number '); Read(Num);
  63.   GotoXY(28,7); Write(Comma2('$',Num,32,2));
  64.   GotoXY(28,8); Write(Comma2('#',Num,32,2));
  65.   GotoXY(28,9); Write(Comma2('%',Num,32,2));
  66. End.
  67.  
  68.